package com.z.kq.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.z.common.aop.annotation.Log;
import com.z.common.aop.enums.BusinessType;
import com.z.common.web.base.BaseController;
import com.z.common.web.vo.request.PageVo;
import com.z.common.web.vo.response.Result;
import com.z.kq.entity.ScheSign;
import com.z.kq.service.IScheSignService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 班制打卡时间Controller
 *
 * @author z
 * @date 2023-03-24
 */
@RestController
@RequestMapping("/kq/scheSign")
public class ScheSignController extends BaseController {

    private String prefix = "kq/scheSign";

    @Autowired
    private IScheSignService scheSignService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/kq/scheSign/main','kq:scheSign:main')")
    public ModelAndView main() {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询班制打卡时间列表
     */
    @ResponseBody
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/kq/scheSign/main','kq:scheSign:main')")
    @Log(title = "班制打卡时间", describe = "查询班制打卡时间列表", type = BusinessType.QUERY)
    public Result list(@ModelAttribute ScheSign scheSign, PageVo pageVo) {
        PageInfo<ScheSign> pageInfo = scheSignService.selectScheSignPage(scheSign, pageVo);
        return Result.table(pageInfo);
    }

    /**
     * 新增班制打卡时间
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/kq/scheSign/add','kq:scheSign:add')")
    public ModelAndView add() {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增班制打卡时间
     */
    @ResponseBody
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/kq/scheSign/add','kq:scheSign:add')")
    @Log(title = "班制打卡时间", describe = "新增班制打卡时间数据", type = BusinessType.ADD)
    public Result save(@RequestBody String body) {
        JSONArray jsonArray = JSONObject.parseArray(body);
        List<ScheSign> list = new ArrayList<>();
        jsonArray.forEach(obj -> {
            ScheSign scheSign = new ScheSign();
            JSONObject o = (JSONObject) obj;
            String unitId = o.getString("unitId");
            if (StringUtils.isNotEmpty(unitId)) {
                scheSign.setUnitId(Long.parseLong(unitId));
            }
            String unitName = o.getString("unitName");
            if (StringUtils.isNotEmpty(unitName)) {
                scheSign.setUnitName(unitName);
            }
            String scheType = o.getString("scheType");
            if (StringUtils.isNotEmpty(scheType)) {
                scheSign.setScheType(scheType);
            }
            String scheName = o.getString("scheName");
            if (StringUtils.isNotEmpty(scheName)) {
                scheSign.setScheName(scheName);
            }
            String scheStage = o.getString("scheStage");
            if (StringUtils.isNotEmpty(scheStage)) {
                scheSign.setScheStage(scheStage);
            }
            String scheSort = o.getString("scheSort");
            if (StringUtils.isNotEmpty(scheSort)) {
                scheSign.setScheSort(scheSort);
            }
            String startTime = o.getString("startTime");
            if (StringUtils.isNotEmpty(startTime)) {
                scheSign.setStartTime(startTime);
            }
            String endTime = o.getString("endTime");
            if (StringUtils.isNotEmpty(endTime)) {
                scheSign.setEndTime(endTime);
            }
            String timeLimitType = o.getString("timeLimitType");
            if (StringUtils.isNotEmpty(timeLimitType)) {
                scheSign.setTimeLimitType(Integer.parseInt(timeLimitType));
            }
            String timeLimit = o.getString("timeLimit");
            if (StringUtils.isNotEmpty(timeLimit)) {
                scheSign.setTimeLimit(timeLimit);
            }
            String timeSort = o.getString("timeSort");
            if (StringUtils.isNotEmpty(timeSort)) {
                scheSign.setTimeSort(timeSort);
            }
            scheSign.setEnable(true);
            list.add(scheSign);
        });
        
        return Result.decide(scheSignService.saveBatch(list));
    }

    /**
     * 修改班制打卡时间
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/kq/scheSign/edit','kq:scheSign:edit')")
    public ModelAndView edit(Long id, ModelMap map) {
        ScheSign scheSign =scheSignService.getById(id);
        map.put("scheSign", scheSign);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改班制打卡时间
     */
    @ResponseBody
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/kq/scheSign/edit','kq:scheSign:edit')")
    @Log(title = "班制打卡时间", describe = "修改班制打卡时间数据", type = BusinessType.EDIT)
    public Result update(@RequestBody ScheSign scheSign) {
        return Result.decide(scheSignService.updateById(scheSign));
    }

    /**
     * 删除班制打卡时间
     */
    @ResponseBody
    @DeleteMapping("/batchRemove")
    @PreAuthorize("hasPermission('/kq/scheSign/del','kq:scheSign:del')")
    @Log(title = "班制打卡时间", describe = "批量删除班制打卡时间数据", type = BusinessType.REMOVE)
    public Result batchRemove(String ids) {
        return Result.decide(scheSignService.removeByIds(Arrays.asList(ids.split(","))));
    }

    /**
     * 删除班制打卡时间
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/kq/scheSign/del','kq:scheSign:del')")
    @Log(title = "班制打卡时间", describe = "删除班制打卡时间数据", type = BusinessType.REMOVE)
    public Result remove(@PathVariable("id") Long id) {
        return Result.decide(scheSignService.removeById(id));
    }
}
